package com.neusoft.elmboot.config;

import com.neusoft.elmboot.interceptor.JwtInterceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("http://localhost:8082") // 前端地址
                        .allowCredentials(true) // 支持携带 Cookie 或 Authorization
                        .allowedHeaders("*") // 允许所有自定义头
                        .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS") // 允许的 HTTP 方法
                        .maxAge(3600); // 预检请求的缓存时间（1小时）
            }
        };
    }

    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns(
                        "/login",
                        "/register",
                        "/BusinessController/listBusinessByOrderTypeId",
                        "/UserController/getUserByIdByPass",
                        "/FoodController/listFoodByBusinessId",
                        "/BusinessController/getBusinessById",
                        "/CartController/listCart",
                        "/DeliveryAddressController/saveDeliveryAddress",
                        "/UserController/saveUser");// 排除不需要验证的路径
    }
}